<?php
/**
 * Created by PhpStorm.
 * User: Webdd
 * Date: 15/4/28
 * Time: 下午2:34
 */

namespace Api\Libs;

class JPush {

    const APPKEY = '9975bc958d8afaef8c565fd9';
    const MASTERSECRET = '204245f940da8f7547693f24';
    const URL = 'https://api.jpush.cn/v3/push';

    /**
     * 极光推送通知
     *
     * @param array  $msg                  推送消息实体  array('content'=>'xxxxx',title='xxxxx' ...)
     * @param string $receiver             接收推送着信息
     *                                     all 对所有用户推送消息
     *                                     tag(20个)Array标签组(并集): tag=>array('昆明','北京','曲靖','上海');
     *                                     tag_and(20个)Array标签组(交集): tag_and=>array('广州','女');
     *                                     alias(1000)Array别名(并集): alias=>array('93d78b73611d886a74*****88497f501','606d05090896228f66ae10d1*****310');
     *                                     registration_id(1000)注册ID设备标识(并集): registration_id=>array('20effc071de0b45c1a**********2824746e1ff2001bd80308a467d800bed39e');
     *
     * @param array $extras                客户端接收参数
     * @param string $m_time               保存离线时间的秒数默认为一天(可不传)单位为秒
     * @return bool|mixed
     */

    public function pushNotification($msg,$receiver='all',$extras=array(),$m_time='86400'){


        $base64=base64_encode(self::APPKEY.":".self::MASTERSECRET);
        $header=array("Authorization:Basic $base64","Content-Type:application/json");

        $data = array();
        $data['platform'] = 'all';          //目标用户终端手机的平台类型android,ios,winphone
        $data['audience'] = $receiver;      //目标用户


        $data['notification'] = array(
            //统一的模式--标准模式
            "alert"=>$msg['content'],
            //安卓自定义
            "android"=>array(
                "alert"=>$msg['content'],
                "title"=>$msg['title'],
                "builder_id"=>1,

                // 定义客户端接收的参数
                "extras"=>$extras
            ),
            //ios的自定义
            "ios"=>array(
                // "alert"=>$content,
                "badge"=>"1",
                "sound"=>"default",
                // "extras"=>$extras
            ),
        );


        //附加选项
        $data['options'] = array(
            "sendno"=>time(),
            "time_to_live"=>$m_time, //保存离线时间的秒数默认为一天
            "apns_production"=>0,        //指定 APNS 通知发送环境：0开发环境，1生产环境。
        );
        $param = json_encode($data);
        $res = $this->push_curl($param,$header);

        if($res){       //得到返回值--成功已否后面判断
            return $res;
        }else{          //未得到返回值--返回失败
            return false;
        }
    }


    /**
     * 极光推送消息
     *
     * @param $msg
     * @param string $receiver
     */
    public function pushMessage($msg,$receiver='all') {

        $base64=base64_encode(self::APPKEY.":".self::MASTERSECRET);
        $header=array("Authorization:Basic $base64","Content-Type:application/json");

        $data = array();
        $data['platform'] = 'all';          //目标用户终端手机的平台类型android,ios,winphone
        $data['audience'] = $receiver;      //目标用户

        $data['message'] = array(
            'msg_content' => $msg['content'],
            'title' => $msg['title'],
        );

        $param = json_encode($data);
        //p($param);
        $res = $this->push_curl($param,$header);

        if($res){       //得到返回值--成功已否后面判断
            return $res;
        }else{          //未得到返回值--返回失败
            return false;
        }
    }



    /**
     * 模拟使用Post方式进行提交
     *
     * @param string $param     提交内容
     * @param string $header    提交头
     * @return mixed
     */
    protected function push_curl($param="",$header="") {

        if (empty($param)) { return false; }

        $postUrl = self::URL;
        $postParam = $param;

        $ch = curl_init();                                      //初始化curl
        curl_setopt($ch, CURLOPT_URL,$postUrl);                 //抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);                    //设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);            //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);                      //post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postParam);
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header);           // 增加 HTTP Header（头）里的字段
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);        // 终止从服务端进行验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $data = curl_exec($ch);                                 //运行curl
        curl_close($ch);
        return $data;
    }

}